<template>
  <div v-loading="isLoadingData">
    <el-row>
      <el-col :span="6">
        <el-statistic title="注册用户数" :value="serverInfo.member" />
      </el-col>
      <el-col :span="6">
        <el-statistic title="帖子数" :value="serverInfo.bbs" />
      </el-col>
      <el-col :span="6">
        <el-statistic title="总资源数" :value="serverInfo.mod" />
      </el-col>
    </el-row>
    <el-divider />
    <el-text size="large">注册新用户</el-text><br /><br />
    <el-button @click="getToken">获取密钥</el-button>
    <el-button link>
      <a href="https://www.schub.wiki/zc.html">点击前往注册</a>
    </el-button>
  </div>
</template>
<script lang="ts">
import Method from '@/globalmethods.ts'
import { ElMessageBox } from 'element-plus'
import { res, serverInfo } from '../type.ts'

export default {
  name: 'admin-site',
  data() {
    return {
      page: 1,
      limit: 10,
      total: 0,
      isLoadingData: false,
      isLocking: false,
      isInjecting: false,
      isPassing: false,
      serverInfo: <serverInfo>{},
    }
  },
  methods: {
    /**
     * 获取服务器信息
     */
    refreshServerInfo() {
      this.isLoadingData = true
      Method.api_get('/admin/server_info').then((response) => {
        this.isLoadingData = false
        let res = <res>response.data
        if (res.code == 200) {
          this.serverInfo = res.data
        }
      })
    },
    /**
     * 获取token
     */
    getToken() {
      Method.api_post('/user/getToken', {})
        .then(async (res: any) => {
          const obj = res.data
          if (obj.code == 200) {
            ElMessageBox.alert(`获取成功：${obj.data.token}`, 'API访问密钥', {
              confirmButtonText: '确定并复制',
              callback: () => {
                Method.copyText(obj.data.token)
              },
            })
          }
        })
        .catch((err: any) => {
          console.log(err)
        })
    },
  },
  mounted() {
    this.refreshServerInfo()
  },
}
</script>
